// pages/edu/timetable/edit.js
const db = wx.cloud.database();
import config from "../../config";
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        classTime:config.classTime,
        days_cn: ['一', '二', '三', '四', '五', '六', '日'],
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        displayArea: app.globalData.displayArea,
        //待输入元素
        //课程
        coursename: '',
        //教室
        courselocation: '',
        //教师名称
        teacher: '',
        checkbox: [],
        //当前周几
        days: 1,
        start_time: 0,
        end_time: 0,
        currday: 0,
        //已选择周次
        checkeds: []
    },
    /**保存选择器信息变动 */
    saveCourseNum() {
        var that = this;
        let data = this.data;
        wx.showLoading({
            title: '数据整理',
            mask: true
        })
        setTimeout(function () {
            that.hideModal();
            wx.hideLoading();
        }, 300)

    },
    /**跳转到课程库 */
    courseHub(){
        wx.navigateTo({
          url: '/pages/tablehub/index',
        })
    },
    /**Picker信息变动监听 */
    sectionChange(e) {
        console.log(e)
        //获取选择状态
        let selarr = e.detail.value;
        this.setData({
            days: selarr[0],
            start_time: selarr[1] + 1,
            end_time: selarr[2] + 1
        })
    },
    /**清空输入信息 */
    emptyCourse() {
        var that = this;
        that.setData({
            sname: '',
            classroom: '',
            jsxm: '',
            checkeds: []
        })
    },
    /**删除自定义课表 */
    delCourse(e) {
        var that = this;
        // console.log(e)
        let item = e.currentTarget.dataset.item;
        let index = e.currentTarget.dataset.index;
        console.log(index)
        console.log(item)
        // 获取ID
        let docId = item._id;
        wx.showLoading({
            title: '删除中',
        })
        wx.cloud.callFunction({
            name: 'edu-timetable-api',
            data: {
                action: 'deleteCustom',
                docId
            },
            success: res => {
                console.log(res)
                let customCourse = this.data.customCourse;
                customCourse.splice(index, 1)
                that.setData({
                    customCourse,
                    succMsg: '删除成功'
                }, () => {
                    if (customCourse.length == 0) {
                        that.hideModal();
                    }
                })
            },
            complete: res => {
                wx.hideLoading();
            }
        })
    },
    /**选择课表 */
    selCourse() {
        //基于原条件进行修改
        wx.showToast({
            title: '功能Building~',
            icon: 'none'
        })
    },
    /**管理自定义课表 */

    saveButton() {
        var that = this;
        /**调用云函数 保存自定义课表 */
        var obj = this.data;
        wx.showLoading({
          title: '上传中~',
          mask:true
        })
        wx.cloud.callFunction({
            name: 'edu-timetable-api',
            data: {
                action: 'addTable',
                days: parseInt(obj.days)+1,
                start_time: obj.start_time,
                end_time: obj.end_time,
                coursename: obj.coursename,
                teacher:obj.teacher,
                courselocation:obj.courselocation,
                weeks: obj.checkeds,
            },
            success:res=>{
                console.log(res)
                if(res.result.errCode == -1){
                    that.setData({
                        errMsg:res.result.errMsg
                    })
                }else{
                    that.setData({
                        succMsg:'上传成功',
                    })
                    /**清空输入 */
                    // that.emptyCourse();
                }
            },
            complete:res=>{
                console.log(res)
                wx.hideLoading();
            }
        })
    },
    ChooseCheckbox(e) {
        let items = this.data.checkbox;
        let values = e.currentTarget.dataset.value;
        for (let i = 0, lenI = items.length; i < lenI; ++i) {
            if (items[i].value == values) {
                items[i].checked = !items[i].checked;
                break;
            }
        }
        this.setData({
            checkbox: items,
        });
    },
    /**
     * 保存课程周数
     */
    saveWeek() {
        let {
            checkbox,
            editId
        } = this.data;
        let checkeds = checkbox.map((v) => {
            if (v.checked) {
                return v.value;
            }
        });
        checkeds = checkeds.filter((v) => v);
        console.log(checkeds);
        this.setData({
            checkeds
        })
        // this.modifyData("attend", editId, checkeds.join(","));
        this.hideModal();
    },

    /**
     * 选择周数
     */
    ChooseWeeks(e) {
        let value = e.currentTarget.dataset.value;
        let checkbox = this.data.checkbox;
        console.log(value);
        const WeekFunc = (type) => {
            return checkbox.map((v) => {
                type == 0 ?
                    (v.checked = true) :
                    type == 1 ?
                    (v.checked = v.value % 2 != 0) :
                    (v.checked = v.value % 2 == 0);
                return v;
            });
        };
        let _temp = WeekFunc(value);
        console.log(_temp, "_temp");
        this.setData({
            ChooseWeek: value,
            checkbox: _temp,
        });
    },
    courseNum() {
        let checkbox = [];
        for (let index = 1; index <= 20; index++) {
            checkbox.push({
                value: index,
                checked: false,
            });
        }
        this.setData({
            checkbox,
        });
    },
    /**
     * 隐藏弹窗
     */
    hideModal() {
        this.setData({
            modalName: null,
            editId: null,
        });
    },
    /**展示弹窗 */
    showModal(e) {
        let {
            index,
            target
        } = e.currentTarget.dataset;
        this.setData({
            modalName: target,
            editId: index,
        });
    },

    /**返回 */
    BackPage() {
        wx.navigateBack()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        let days_cn = ['一', '二', '三', '四', '五', '六', '日']
        this.setData({
            days: options.day,
            currday: parseInt(options.day) + 1,
            start_time: parseInt(options.nums) + 1,
            end_time: parseInt(options.nums) + 2
        })
        this.courseNum();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})